package cn.wuxing.mediation.web.controller.infoconfig;

import cn.wuxing.mediation.common.annotation.Log;
import cn.wuxing.mediation.common.core.controller.BaseController;
import cn.wuxing.mediation.common.core.domain.AjaxResult;
import cn.wuxing.mediation.common.core.page.TableDataInfo;
import cn.wuxing.mediation.common.enums.BusinessType;
import cn.wuxing.mediation.infoconfig.domain.dto.AddWindowUserDTO;
import cn.wuxing.mediation.infoconfig.domain.dto.EditWindowDTO;
import cn.wuxing.mediation.infoconfig.domain.dto.RemoveWindowUserDTO;
import cn.wuxing.mediation.infoconfig.domain.vo.IcWindowListVO;
import cn.wuxing.mediation.infoconfig.domain.vo.WindowUserVO;
import cn.wuxing.mediation.infoconfig.service.IIcWindowService;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 窗口信息
 *
 * @author mediation
 */
@Slf4j
@RestController
@RequestMapping("/infoconfig/window")
public class IcWindowController extends BaseController {

    @Autowired
    private IIcWindowService iIcWindowService;

    /**
     * 获取部门列表
     */
    @GetMapping("/list")
    public AjaxResult list(String windowName) {
        List<IcWindowListVO> depts = iIcWindowService.selectWindowList(windowName);
        return success(depts);
    }

    /**
     * 删除窗口
     */
    @Log(title = "专窗管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        if (iIcWindowService.checkWindowExistUser(id)) {
            return warn("专窗存在用户,不允许删除");
        }
        return toAjax(this.iIcWindowService.del(id));
    }

    /**
     //     * 专窗管理
     //     */
    @Log(title = "专窗管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@Valid @RequestBody EditWindowDTO dto) {
        if (this.iIcWindowService.checkNameUnique(dto.getId(), dto.getWindowName())) {
            return error("修改窗口'" + dto.getWindowName() + "'失败，窗口名称已存在");
        }
        return toAjax(this.iIcWindowService.update(dto));
    }

    /**
     //     * 专窗管理
     //     */
    @Log(title = "专窗管理", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult insert(@Valid @RequestBody EditWindowDTO dto) {
        if (this.iIcWindowService.checkNameUnique(dto.getId(), dto.getWindowName())) {
            return error("修改窗口'" + dto.getWindowName() + "'失败，窗口名称已存在");
        }
        return toAjax(this.iIcWindowService.insert(dto));
    }

    /**
     * 获取详细信息
     */
    @GetMapping(value = "/detail/{id}")
    public AjaxResult detail(@PathVariable Long id) {
        return success(this.iIcWindowService.detail(id));
    }

    /**
     * 专窗用户
     *
     * @param windowId
     * @return
     */
    @GetMapping("/getWindowUserList")
    public TableDataInfo getWindowUserList(@NotNull(message = "windowId不能为空") Long windowId) {
        startPage();
        List<WindowUserVO> list = this.iIcWindowService.getWindowUserList(windowId);
        log.info("IIcWindowService_getWindowUserList_list_{}", list);
        return getDataTable(list);
    }

    /**
     //     * 新增专窗用户
     //     */
    @Log(title = "专窗管理", businessType = BusinessType.INSERT)
    @PostMapping("/addWindowUser")
    public AjaxResult addWindowUser(@Validated @RequestBody AddWindowUserDTO dto) {
        if (this.iIcWindowService.checkWindowUserUnique(dto)) {
            return error("该用户已存在");
        }
        this.iIcWindowService.addWindowUser(dto);
        return AjaxResult.success();
    }

    /**
     //     * 移除专窗用户
     //     */
    @Log(title = "专窗管理", businessType = BusinessType.INSERT)
    @PostMapping("/removeWindowUser")
    public AjaxResult removeWindowUser(@Validated @RequestBody RemoveWindowUserDTO dto) {
        this.iIcWindowService.removeWindowUser(dto);
        return AjaxResult.success();
    }
}
